﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ScheduleManagement.DAL
{
    public class MainFrom_Dal
    {
        //读取配置文件,配置数据库连接字符串
        string cn = Properties.Settings.Default.ConnectionString;

        public System.Data.DataSet List(int flag,int u_Id)
        {
            string commandText = "p_Dailytask";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@hc_Option", flag);
            commandParameters[1] = new SqlParameter("@u_Id", u_Id);
            System.Data.DataSet ds = SqlHelper.ExecuteDataset(cn,CommandType.StoredProcedure,commandText,commandParameters);
            return ds;
        }

        #region 查询区下拉列表框数据绑定
        public SqlDataReader ListRemind()
        {
            string commandText = "p_Remindtype";
            System.Data.SqlClient.SqlDataReader dr= SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, commandText);
            return dr;
        }

        public SqlDataReader ListSqType()
        {
            string commandText = "p_Schedulequlity";
            System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, commandText);
            return dr;
        }

        public SqlDataReader ListStpType()
        {
            string commandText = "p_Scheduletype";
            System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, commandText);
            return dr;
        }

        public SqlDataReader ListStatus()
        {
            string commandText = "p_Status";
            System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, commandText);
            return dr;
        }

        #endregion


        public DataSet SigleSelect(bool p_HideFinish, string p,string type)
        {
            string commandText = "";
            switch (p)
            {
                case "Remind":
                    commandText = "p_sRemindtype";
                    break;
                case "SqType":
                    commandText = "p_sSchedulequlity";
                    break;
                case "StpType":
                    commandText = "p_sScheduletype";
                    break;
                case "Cascade":
                    commandText = "p_sCascade";
                    break;
                case "Announ":
                    commandText = "p_sAnnoun";
                    break;
                case "Status":
                    commandText = "p_sStatus";
                    break;
                default:
                    break;

            }
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@hc_Option", p_HideFinish);
            commandParameters[1] = new SqlParameter("@temp", type);
            commandParameters[2] = new SqlParameter("@u_Id", ScheduleManagement.MODEL.Users.U_Id);
            System.Data.DataSet ds = SqlHelper.ExecuteDataset(cn, CommandType.StoredProcedure, commandText, commandParameters);
            return ds;
            



        }

        public System.Data.DataSet MergeSelect(bool p_HideFinish, string p)
        {

            //测试
            string commandText = "p_Dailytask";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@hc_Option", p_HideFinish);
            System.Data.DataSet ds = SqlHelper.ExecuteDataset(cn, CommandType.StoredProcedure, commandText, commandParameters);
            return ds;
        }

        /// <summary>
        /// 查询并返回当前用户总日程
        /// </summary>
        /// <returns></returns>
        public DataSet ListAll()
        {
            string commandText = "p_UserSchedule";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@u_Id", ScheduleManagement.MODEL.Users.U_Id);
            System.Data.DataSet ds = SqlHelper.ExecuteDataset(cn, CommandType.StoredProcedure, commandText, commandParameters);
            return ds;
        }

        //public ScheduleManagement.MODEL.Schedulequality GetStpId(string p)
        //{
        //    throw new Exception("The method or operation is not implemented.");
        //}

        //public ScheduleManagement.MODEL.Schedulequality GetSqId(string p)
        //{
        //    throw new Exception("The method or operation is not implemented.");
        //}

        public string GetStpId(string p)
        {
            string commandText = "p_selectstptype";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@stp_Type", p);
            return SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText, commandParameters).ToString();
           
        }

        public string GetSqId(string p)
        {
            string commandText = "p_selectsqtype";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@sq_Type", p);
            return SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText, commandParameters).ToString();
        }

        public void AddSchedule(ScheduleManagement.MODEL.Schedule sch)
        {
            string commandText = "p_AddSchedule";
            SqlParameter[] commandParameters = new SqlParameter[10];
            commandParameters[0] = new SqlParameter("@u_Id",ScheduleManagement.MODEL.Users.U_Id);
            commandParameters[1] = new SqlParameter("@sq_Id",sch.Schedulequality.Sq_Id );
            commandParameters[2] = new SqlParameter("@stp_Id", sch.Scheduletype.Stp_Id);
            commandParameters[3] = new SqlParameter("@s_Quality",1 );
            commandParameters[4] = new SqlParameter("@s_Content", sch.S_Content);
            commandParameters[5] = new SqlParameter("@r_Id", sch.Remindtype.R_Id);
            commandParameters[6] = new SqlParameter("@st_Id",sch.Status.St_Id );
            commandParameters[7] = new SqlParameter("@s_Announcement", sch.S_Announcement);
            commandParameters[8] = new SqlParameter("@s_Cascade", sch.S_Cascade);
            commandParameters[9] = new SqlParameter("@s_Voice", sch.S_Voice);
            SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText, commandParameters);
            
        }
    }
}
